/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#pragma once

/*-----------------------------------------------------------------------------
	Game main module :
-----------------------------------------------------------------------------*/

const uint SHIP_MAX_VORTS	=	1024;

typedef HardRef<class EGShip>	EPxGShip;


class EGShip : public ICoreObject {
	public:
						EGShip			( lua_State *L, int tidx );
						~EGShip			( void );

		void			SetPose			( const EPoint &pos, const EQuaternion &orient );
		void			GetPose			( EPoint &pos, EQuaternion &orient );
		void			Update			( float dtime );
		
		void			ApplyForces		( const EVector &force, const EPoint &pos );
		
		void			UpdateForces	( void );
		
	public:
		float	life_time;
		EBBox	ship_bbox;
	
		struct ShipDynamics {
			float		time;
			EVector		force;
			EVector		torque;
			EPoint		position;
			EQuaternion	orient;
			float		ship_length;
			float		ship_width;
			float		ship_mass;
			float		wave_height;
		};

		ShipDynamics	GetShipDynamics	( void );

	protected:
		ERandMT	mtrand;
	
		void			UpdateHXFGrid	( float density=0 );
		void			UpdateHXF		( HardRef<EGWaving> waving );
		
		float			screw_angle;
		void			UpdateScrews	( void );
		
		struct SurfElem {
			EPoint	position;
			EVector	normal;
			float	area;
			bool	submerged;
		};
	
		//	meshes :
		IPxTriMesh		hxf_mesh;
		IPxTriMesh		vis_mesh;
		IPxTriMesh		phys_mesh;
		float			mass;
		
		IPxPhysEntity	phys_body;
		IPxFREntity		rend_model;
		IPxFREntity		rend_screwL;
		IPxFREntity		rend_screwR;
		
		IPxFRGFX		gfx_vorts;
		
		IPxTriMesh		turb_mesh;
		IPxFREntity		turb_rend;
		
		IPxFRLight		light;

		//	simulation params :		
		float			integral_density;
		float			water_resistance;
		float			water_resistance2;
		float			water_viscosity;

		struct Vortex {
			Vortex() { lifetime=0, total_lifetime=0; dead=true; }
			bool  dead;
			float angle;
			float angular_velocity;
			float lifetime;
			float total_lifetime;
			float radius;
			EVector vel;
			EPoint pos;
		};
		
		Vortex	vorticies[SHIP_MAX_VORTS];
		
		void	DrawVorticies	( float dtime, HardRef<EGWaving> waving );
		void	AddVorticies	( float x, float y, float z, float total_lifetime, float vx, float vy, float r );

		//	config :		
		IPxVar			ship_show_grid;
		IPxVar			ship_disable_simulation;
		
		struct {
			std::vector<SurfElem>	grid;
		} hxfgrid;

		// Sound stuff
		IPxSound3D engine_snd;
	};